<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>新增借用紀錄</title>
</head>
<body>
<% if(session.getAttribute("access") == "y" ){%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<jsp:useBean id="sendmail" scope="page" class="bean.SendMail"/><%//寄發mail %>
<jsp:useBean id="member" scope="page" class="myclass.Member"/> <%//取得會員資料 %>
<jsp:useBean id="getPlace" scope="page" class="myclass.Place"/> <%//取得場地資料 %>
<%
String pid = new String(request.getParameter("p_id").getBytes("iso-8859-1"),"utf-8"); //防止亂碼
String bdate=new String(request.getParameter("book_date").getBytes("iso-8859-1"),"utf-8");
String btime=new String(request.getParameter("bt_class").getBytes("iso-8859-1"),"utf-8");
String mid= new String(request.getParameter("m_id").getBytes("iso-8859-1"),"utf-8");
String breason= new String(request.getParameter("reason").getBytes("iso-8859-1"),"utf-8");

String sql="INSERT INTO BOOK(M_ID,PF_ID,BOOK_REASON,BOOK_TIME,APPLY_DATE) VALUES('"+mid+"','"+pid+"','"+breason+"','"+btime+"','"+bdate+"')";
mydb.executeInsert(sql);
//F_STATE=10 表示"借用中"
sql="UPDATE PLACE SET P_STATE='01' WHERE P_ID='"+pid+"'";
mydb.executeUpdate(sql);

/*寄信通知會員預借事項*/
member.setMember(mid);
sendmail.get_pto(member.getEmail());//取得會員信箱
sendmail.setSubject("book");//郵件標題
getPlace.setPlace(pid);
String sqlmail="select * from SYS_SETTING WHERE S_NAME='book_text'";
      ResultSet rs=mydb.executeQuery(sqlmail);
      if(rs.next()){
      	String mag = "";
      	mag = rs.getString(3);
        mag = mag+"您預借的場地為["+getPlace.getPName()+"] 您預借的日期時段為["+bdate+"-"+btime+"時段]";    
		sendmail.setText(mag);
		sendmail.sendMail();
      }
response.sendRedirect("lookBook.jsp"); //直接跳頁到lookBook.jsp
%>

<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>